package com.example.aiyou.passwordmanage.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.aiyou.passwordmanage.R;
import com.example.aiyou.passwordmanage.adapter.DetailAdpater;
import com.example.aiyou.passwordmanage.adapter.RecycleViewDivider;
import com.example.aiyou.passwordmanage.base.BaseActivity;
import com.example.aiyou.passwordmanage.pojo.DetailShow;
import com.example.aiyou.passwordmanage.pojo.DetailView;
import com.example.aiyou.passwordmanage.pojo.Password;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class DetailActivity extends BaseActivity {
    private Password password;
    private List<DetailView> detailViewList = new ArrayList<DetailView>();
    private AddActivity addActivity;
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_edit:
                    Intent intent = new Intent(DetailActivity.this,AddActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("password",password);
                    bundle.putInt("mark",1);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
            }
            return true;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        final Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        password = (Password) bundle.getSerializable("password");
        setTitle(password.getName());
        initList();
        RecyclerView recyclerView = (RecyclerView) findViewById( R.id.deatil_show);
        LinearLayoutManager layoutManager = new LinearLayoutManager( this);
        recyclerView.setLayoutManager( layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(DetailActivity.this, LinearLayoutManager.HORIZONTAL));
        DetailAdpater detailAdpater = new DetailAdpater(detailViewList,password);
        recyclerView.setAdapter(detailAdpater);
        detailAdpater.setOnItemClickListener(new DetailAdpater.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }

            @Override
            public void onLongClick(View view, int position) {
                ClipboardManager cmb = (ClipboardManager)DetailActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                String text = detailViewList.get(position).getText().trim();
                if(null != text || text.isEmpty()){
                    cmb.setText(detailViewList.get(position).getText().trim());
                    Toast.makeText(DetailActivity.this,"已复制",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    public static void actionStart(Context context){
        Intent intent = new Intent(context,DetailActivity.class);
        context.startActivity(intent);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_detail, menu);
        return true;
    }
    public static void actionStart(Context context, Password password){
        Intent intent = new Intent(context,DetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("password",password);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
    public void initList(){
        DetailShow [] array =  DetailShow.values();
        Resources res = getResources();
        String[] items = res.getStringArray(R.array.spingarr);
        for(DetailShow d : array){
            DetailView detailView = new DetailView();
            detailView.setDisplayName(d.getDiaplayName());
            Field field = null;
            try {
                field = password.getClass().getDeclaredField(d.getKey());
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            field.setAccessible(true);
            try {
                String text = "";
                if(d.getKey().equals("type")){
                    int i = (int) field.get(password);
                    text = items[i];
                }else{
                    text = (String) field.get(password);
                }
                detailView.setText(text);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            detailViewList.add(detailView);
        }
        System.out.println(detailViewList.size());
    }

}
